package com.yycx.module.bus.provider.handler.haiying.settle.apply;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.module.export.DictDataVo;
import com.yycx.common.base.module.export.ExportField;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.BusHyorderReturn;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.enums.DeptTypeEnum;
import com.yycx.module.system.client.entity.SysCompany;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 结算费用申请-tab1
 *
 * @author zyf
 */
@Component
public class HySettleApplyTab1PageListHandler implements PageInterceptor<BusHyorderReturn>, ExportInterceptor {


    @Override
    public ResultBody validate(CriteriaQuery<BusHyorderReturn> cq, EntityMap params) {
        //销售角色
        Boolean xs = OpenHelper.hasRole("ROLE_XS_KPY", "ROLE_XS_JSY");
        //市场组角色
        Boolean sw = OpenHelper.hasRole("ROLE_SW_JSY");
        if (!xs && !sw) {
            return ResultBody.failed("无结算权限");
        }
        return PageInterceptor.super.validate(cq, params);
    }

    @Override
    public void prepare(CriteriaQuery<BusHyorderReturn> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        String saleManName = params.get("saleManName");
        String orderNo = params.get("orderNo");
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = openUser.getCompanyId();
        cq.addSelect(HyOrder.class, "orderNo", "outOrderNo","sendDate", "businessCode", "sendType", "customBondType", "saleManName");
        cq.addSelect(BusHyorderReturn.class, "*");
        cq.addSelect(BusCustom.class, "customName", "settleType");
        cq.addSelect(SysCompany.class, "companyShortName");
        cq.addSelect(BusDept.class, "deptName", "deptType");
        cq.eq("ho.orderState", CommonConstants.INT_2);
        cq.like("ho.saleManName", saleManName);
        //发货单号
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.like("ho.orderNo", orderNo).or().like("ho.outOrderNo", orderNo));
        }
        cq.like(BusCustom.class, "customName");
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "ho.sendDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "ho.sendDate", endDate);
        cq.eq(true, "ho.companyId", companyId);
        cq.eq(HyOrder.class, "deptId");

        //销售角色
        Boolean xs = OpenHelper.hasRole("ROLE_XS_KPY", "ROLE_XS_JSY");
        //市场组角色
        Boolean sw = OpenHelper.hasRole("ROLE_SW_JSY");
        //销售角色
        if (xs && !sw) {
            cq.eq("dept.deptType", DeptTypeEnum.ZS.getCode());
        }
        //商务角色
        if (sw && !xs) {
            cq.eq("dept.deptType", DeptTypeEnum.ZY.getCode());
        }
        //费用已核算
        cq.eq("ho.calculateState", CommonConstants.INT_2);
        //部门费用申请已提交
        cq.isNotNull("bhr.deptSettleApplyId");
        //结算条件
        cq.and(e -> e.nested(i -> i.isNull("settleApplyId").ne("settleAmount", 0)).or().nested(j -> j.isNull("settleApplyIdC").ne("settleAmountC", 0)).or().nested(j -> j.isNull("settleApplyIdD").ne("settleAmountD", 0)).or().nested(j -> j.isNull("settleApplyIdE").ne("settleAmountE", 0)));
        //已回款或者允许未回款结算的订单
        //cq.and(e -> e.eq("custom.allowJs", "1").or().isNotNull("bhr.returnId"));

        cq.createJoin(BusCustom.class);
        cq.createJoin(BusDept.class);
        cq.createJoin(HyOrder.class);
        cq.createJoin(SysCompany.class);
    }

    @Override
    public void initExcelExportEntity(ExportField exportField, List list) {
        Object key = exportField.getKey();
        if (key.equals("deptType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("自营");
            dictDataVo.setValue("ZY");
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("招商");
            dictDataVo2.setValue("ZS");
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
        if (key.equals("sendType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("现款");
            dictDataVo.setValue(1);
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("预付款");
            dictDataVo2.setValue(2);
            DictDataVo dictDataVo3 = new DictDataVo();
            dictDataVo3.setLabel("资保金");
            dictDataVo3.setValue(3);
            DictDataVo dictDataVo4 = new DictDataVo();
            dictDataVo4.setLabel("资信");
            dictDataVo4.setValue(4);
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            dictDataVos.add(dictDataVo3);
            dictDataVos.add(dictDataVo4);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
    }

}
